package com.jasonjan.jiance.module.news;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.jasonjan.jiance.R;
import com.jasonjan.jiance.module.base.BasePagerAdapter;
import com.jasonjan.jiance.test.Test_Fragment;
import com.jasonjan.jiance.util.SettingUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.support.annotation.Nullable;

import io.reactivex.Observable;


/**
 * Created by JasonJan on 2017/12/1.
 */

public class NewsTabLayout extends Fragment {
     public static final String TAG="NewsTabLayout";
     private static NewsTabLayout instance=null;
     private ViewPager viewPager;
     private BasePagerAdapter adapter;
     private LinearLayout linearLayout;
     //private NewsChannelDao dao=new NewsChannelDao();
     private List<Fragment> fragmentList;
     private List<String> titleList;
     private Observable<Boolean> observable;
     private Map<String,Fragment> map=new HashMap<>();

     public static NewsTabLayout getInstance(){
         if(instance==null){
             instance=new NewsTabLayout();
         }
         return instance;
     }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news_tab, container, false);
        initView(view);
        initData();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        linearLayout.setBackgroundColor(SettingUtil.getInstance().getColor());
    }

     public void initView(View view){
         TabLayout tab_layout=view.findViewById(R.id.tab_layout_news);
         viewPager=view.findViewById(R.id.view_pager_news);

         tab_layout.setupWithViewPager(viewPager);
         tab_layout.setTabMode(TabLayout.MODE_SCROLLABLE);
         ImageView add_channel_iv=view.findViewById(R.id.add_chnnel_iv);
         add_channel_iv.setOnClickListener(new View.OnClickListener(){
             @Override
             public void onClick(View v){
                 //设置点击事件
             }
         });
         linearLayout=view.findViewById(R.id.header_layout);
         linearLayout.setBackgroundColor(SettingUtil.getInstance().getColor());
     }

     public void initData(){
         fragmentList = new ArrayList<>();
         titleList = new ArrayList<>();
         fragmentList.add(new Test_Fragment());
         titleList.add("推荐");
         fragmentList.add(new Test_Fragment());
         titleList.add("热点");
         fragmentList.add(new Test_Fragment());
         titleList.add("视频");
         fragmentList.add(new Test_Fragment());
         titleList.add("社会");
         fragmentList.add(new Test_Fragment());
         titleList.add("娱乐");

         adapter = new BasePagerAdapter(getChildFragmentManager(), fragmentList, titleList);
         viewPager.setAdapter(adapter);
         viewPager.setOffscreenPageLimit(15);

     }
}
